<?php 
session_start();

if(empty($_SESSION["log"])){
    header('Location: login.php');
}

include 'connexion.php';

if(isset($_GET['id'])){
    $id_user = $_GET['id'];
}



    $sql = "SELECT * FROM bien WHERE id_user=".$id_user;
    $req = mysqli_query($db,$sql) or die('Erreur SQL !'.$sql.'<br>'.mysqli_error($db)); 
    

?> 

<html>
    <head>
          <link rel="stylesheet" type="text/css" href="style.css"/>
      <meta charset="utf-8" />
      <meta name="description" content="Free Bed ou comment trouver un endroit pour dormir en 2 cliques."/>
      <title>Free Bed</title>
    </head>
    <body>
      <div id="conteneur">
            <div id="header">
                <?php include_once("header.php"); ?>
        </div><br/>
            <div id="contenu">
                <?php 
                        while ( $data= mysqli_fetch_array($req))
                          { 

                                $sql_type = "SELECT type FROM type where id=".$data['id_type'];
                                $res_type = mysqli_query($db,$sql_type) or die('Erreur SQL !'.$sql_type.'<br>'.mysqli_error($db)); 
                                $data1 = mysqli_fetch_array($res_type);

                                $sql_tarif = "SELECT * FROM tarif where id=".$data['id_tarif'];
                                $res_tarif = mysqli_query($db,$sql_tarif) or die('Erreur SQL !'.$sql_tarif.'<br>'.mysqli_error($db));
                                $data2 = mysqli_fetch_array($res_tarif);

                                $sql_pays = "SELECT pays FROM pays where id=".$data['id_pays'];
                                $res_pays = mysqli_query($db,$sql_pays) or die('Erreur SQL !'.$sql_pays.'<br>'.mysqli_error($db)); 
                                $data3 = mysqli_fetch_array($res_pays);

                                $sql_user = "SELECT nom FROM user where id=".$data['id_user'];
                                $res_user = mysqli_query($db,$sql_user) or die('Erreur SQL !'.$sql_user.'<br>'.mysqli_error($db));
                                $data4 = mysqli_fetch_array($res_user);

                                $sql_photo = "SELECT photo FROM photo where id_bien=".$data['id'];
                                $res_photo = mysqli_query($db,$sql_photo) or die('Erreur SQL !'.$sql_photo.'<br>'.mysqli_error($db));
                                $data5 = mysqli_fetch_array($res_photo);

                                if ($data5['photo'] == "") {
                                    $sql_photo = "SELECT photo FROM photo where id_bien=0";
                                    $res_photo = mysqli_query($db,$sql_photo) or die('Erreur SQL !'.$sql_photo.'<br>'.mysqli_error($db));
                                    $data5 = mysqli_fetch_array($res_photo);
                                }

                                $sql_date = "SELECT * FROM reservation where id_bien=".$data['id'];
                                $res_date = mysqli_query($db,$sql_date) or die('Erreur SQL !'.$sql_date.'<br>'.mysqli_error($db));
                                $data6 = mysqli_fetch_array($res_date);

                                if(isset($_POST['supprimer'])){
                                    $id_bien = $data['id'];
                                    $query = "DELETE FROM bien WHERE id=".$id_bien;
                                    mysqli_query($db,"DELETE FROM bien WHERE id=".$id_bien);
                                    echo $query;
                                    //header('Location: index.php');
                                }

                                if ($data6['confirmation']==1) 
                                {
                                    $_SESSION['louer'] = "Ce bien est louée";
                                }
                                else
                                {
                                    $_SESSION['louer'] = "Ce bien n'est pas louée";
                                }

                                $sql_ville = "SELECT ville FROM ville where id=".$data['id_ville'];
                                $res_ville = mysqli_query($db,$sql_ville) or die('Erreur SQL !'.$sql_ville.'<br>'.mysqli_error($db)); 
                                $data7 = mysqli_fetch_array($res_ville);

                                echo "  <table border='2'>
                                            <tr>
                                                <td rowspan=7>
                                                    <img src=".$data5['photo'].">
                                                </td>
                                                <td>
                                                    Description : ".stripslashes($data['description'])."
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Adresse : ".$data['adresse']."
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Lieu : ".$data7['ville'].", ".$data3['pays']."
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Surface : ".$data['surface']." m²
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Type : ".$data1['type']."
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Tarif par jour : ".$data2['tarif_jour']." €
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Tarif par semaine : ".$data2['tarif_semaine']." €
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Loué par : ".$data4['nom']."
                                                </td>
                                                <td>
                                                    <a href='edit_annonce.php?id=".$data['id']."'>Modifier l'annonce</a>
                                                </td>
                                            </tr>
                                        </table>";
                          }
                ?>
            </div>
            <div id="footer">
                <?php include_once("footer.html"); ?>
            </div>
      </div>
    </body>
</html>